package com.stylefeng.guns.rest.modular.auth.validator.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.rest.common.persistence.dao.DriverInfMapper;
import com.stylefeng.guns.rest.common.persistence.model.DriverInf;
import com.stylefeng.guns.rest.modular.auth.validator.IDriverInfService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 系统字典表 Service 实现
 * @author 张亚林
 * @Date 2018-11-07 10:02
 */
@Service
public class DriverInfServiceImpl extends ServiceImpl<DriverInfMapper, DriverInf> implements IDriverInfService {

    @Resource
    private DriverInfMapper driverInfMapper;

    /**
     * 删除系统字典表
     * @param driverInf
     * @return
     */
    @Transactional
    public int deleteDriverInf(DriverInf driverInf,String token)throws Exception{
        if(driverInf==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.driverInfMapper.deleteDriverInf(driverInf);
    }
    /**
     * 批量删除系统字典表
     * @param ids 主键ID数组
     * @return
     */
    @Transactional
    public int deleteBatchDriverInf(String[] ids,String token)throws Exception{
        return this.driverInfMapper.deleteBatchDriverInf(ids);
    }
    /**
     * 查询系统字典表列表
     * @param driverInf 系统字典表对象
     * @return
     */
    public List<DriverInf> selectListDriverInf(DriverInf driverInf,String token)throws Exception{
        return  this.driverInfMapper.selectListDriverInf(driverInf);
    }
    /**
	 * 分页查询系统字典表列表
     * @param pager 分页对象
     * @return
     */
    public  Map pageListDriverInf(Pager pager,String token) throws Exception{
        Page<DriverInf> page=new Page<>(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(driverInfMapper.selectListDriverInf(page,(DriverInf)pager.getObj()));
        List<DriverInf> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param driverInf 系统字典表对象
     * @return
     */
    public  List<DriverInf> selectListDriverInfForUpdate(DriverInf driverInf)throws Exception{
        return  this.driverInfMapper.selectListDriverInfForUpdate(driverInf);
    }
    /**
     * 通过主键id查询的系统字典表列表
     * @param id 主键id
     * @return
     */
    public  DriverInf findDriverInfById(String id,String token)throws Exception{
        return  this.driverInfMapper.findDriverInfById(id);
    }

    /**
     * 新增系统字典表数据
     * @param driverInf  系统字典表对象
     * @return
     */
    @Transactional
    public int insertDriverInf(DriverInf driverInf,String token) throws Exception {
        if(driverInf!=null && (driverInf.getDiId()==null || driverInf.getDiId().trim().length()<10)){
            driverInf.setDiId(IdWorker.get32UUID());
        }
        return this.driverInfMapper.insertDriverInf(driverInf);
    }

    /**
     * 更新系统字典表数据
     * @param driverInf 系统字典表对象
     * @return
     */
    @Transactional
    public int updateDriverInf(DriverInf driverInf,String token) throws Exception {
        return  this.driverInfMapper.updateDriverInf(driverInf);
    }
    /**
     * 查询系统字典表返回key-Value属性
     * @param driverInf 系统操作对象
     * @return
     */
    public List<CommonKeyValue> optionsDriverInf(DriverInf driverInf,String token) throws Exception {
        List<DriverInf> listDriverInf=this.driverInfMapper.selectListDriverInf(driverInf);
        List<CommonKeyValue> listOptions=new ArrayList<>();
        listDriverInf.forEach(item->{
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getDiId());
            opts.setValue(item.getDiName());
            listOptions.add(opts);
        });
        return listOptions;
    }

    /**
     * 更细驾驶员任务数
     * @param driverInf
     * @return
     * @throws Exception
     */
    @Transactional
    public int updateDriverInfTaskNum(DriverInf driverInf, String token) throws Exception {
        if(driverInf==null || driverInf.getDiId()==null|| driverInf.getDiId().trim().length()<10
                || driverInf.getDiTaskNum()==null){
            throw new RuntimeException("更新驾驶员任务数参数不全！");
        }
        this.driverInfMapper.updateDriverInfTaskNum(driverInf);
        return 0;
    }
}
